package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.repository.ICommentsRepository;
import cn.wolfcode.luowowo.comment.repository.IScenicCommentRepository;
import cn.wolfcode.luowowo.common.domain.Comments;
import cn.wolfcode.luowowo.common.domain.ScenicComment;
import cn.wolfcode.luowowo.common.query.ScenicCommentObject;
import cn.wolfcode.luowowo.common.service.IScenicCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {

    @Autowired
    private IScenicCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private ICommentsRepository commentsRepository;

    @Override
    public void saveComment(ScenicComment comment) {
        if(!StringUtils.hasLength(comment.getId())){
            comment.setCreateTime(new Date());
        }
        repository.save(comment);
    }

    @Override
    public Page query(ScenicCommentObject qo) {
        //查询条件
        Query query = new Query();

        //攻略id条件(相当于SQL里的where detailId = #{detailId})
        if(qo.getDetailId()!= -1){
            query.addCriteria(Criteria.where("detailId").is(qo.getDetailId()));
        }

        //查询总数
        long count = template.count(query, ScenicComment.class);
        if(count == 0){
            return Page.empty();
        }

        //参数1:当前页  注意从0开始
        //参数2:每页显示条数
        //参数3:排序规则  第一个参数表示倒序  第二个参数表示按哪个字段排序
        PageRequest request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.by(Sort.Direction.DESC, "thumbupnum"));
        query.with(request);

        List<ScenicComment> list = template.find(query, ScenicComment.class);

        return new PageImpl(list,request,count);
    }

    @Override
    public void reply(Comments comments) {
        if(!StringUtils.hasLength(comments.getId())){
            comments.setCreateTime(new Date());
        }
        if(comments.getType() == ScenicComment.SCENIC_COMMENT_TYPE_AGAIN){
            Optional<Comments> op = commentsRepository.findById(comments.getComments().getId());
            comments.setComments(op.get());
        }
        commentsRepository.save(comments);
    }

    @Override
    public Page<Comments> getComments(ScenicCommentObject qo) {
        //查询条件
        Query query = new Query();

        //攻略id条件(相当于SQL里的where detailId = #{detailId})
        if(qo.getDetailId()!= -1){
            query.addCriteria(Criteria.where("detailId").is(qo.getDetailId()));
        }
        //查询总数
        long count = template.count(query, Comments.class);
        if(count == 0){
            return Page.empty();
        }

        PageRequest request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.by(Sort.Direction.DESC,"createTime"));
        query.with(request);

        List<Comments> list = template.find(query, Comments.class);
        return new PageImpl<>(list,request,count);
    }
}
